package com.ruoyi.school;

import com.ruoyi.school.event.InsertLeaveEvent;
import com.ruoyi.school.event.InsertStudentEvent;
import com.ruoyi.school.event.InsertTeacherEvent;
import com.ruoyi.school.event.SelectCourseEvent;
import org.openapi.consts.EventType;
import org.openapi.event.EventBus;
import org.openapi.service.IApiLoaderService;
import org.openapi.event.ApiEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 启动加载api模型和配置
 */
@Component
public class ApiRunner implements ApplicationRunner {
    @Autowired
    private IApiLoaderService apiLoaderService;
    @Autowired
    private ApiEventService apiEventService;
    @Autowired
    private InsertTeacherEvent insertTeacherEvent;
    @Autowired
    private InsertStudentEvent insertStudentEvent;
    @Autowired
    private InsertLeaveEvent insertLeaveEvent;
    @Autowired
    private SelectCourseEvent selectCourseEvent;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        apiLoaderService.decideDb();
        apiLoaderService.loadModel();
        apiLoaderService.loadEvent();
        apiLoaderService.loadSql();
        apiLoaderService.loadExchange();

        apiEventService.initApiEvent();

        EventBus.on("base_teacher", EventType.BI, insertTeacherEvent);
        EventBus.on("base_student", EventType.BI, insertStudentEvent);
        EventBus.on("oa_leave", EventType.BI, insertLeaveEvent);
        EventBus.on("teach_student", EventType.BI, selectCourseEvent);
    }
}
